//
//  WelcomeViewController.swift
//  微博三天
//
//  Created by jessica on 15/11/13.
//  Copyright © 2015年 jessica. All rights reserved.
//

//欢迎界面

import UIKit

import SnapKit

import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    func startAnimation(){
        
        //让iconImage有个动画效果,修改约束
        self.iconImage.snp_updateConstraints { (make) -> Void in
            
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height
            + 180)
            
        }
        
        
        /*
        一般在自动布局中使用动画效果  不建议直接修改frame
        
        自动布局系统 会根据设置的约束 在layoutSubViews方法中自动修改frame
        frame 是有自动布局系统来控制的
        如果希望能够提前刷新修改的约束效果  需要调用 layoutIfNeeded() 让约束提前刷新 ,更新frame
        */
        
        
            //usingSpringWithDamping  弹簧系数  0~1之间  越小越弹
    UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9,
            //弹簧效果 和加速度之间的关系: 弹簧效果 * 10 = 加速度
        initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
        
//        提前收集到约束变化,进行更新
        self.view.layoutIfNeeded()
    
        }) { (_) -> Void in
            
            
           UIView.animateWithDuration(1, animations: { () -> Void in
            
            
            self.label.alpha = 1
            
            }, completion: { (_) -> Void in
                
                
                //动画完成后,发个通知
                
                NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
//                print("Yeah")
           })
            
        }
    
    
    
    }
    
    override func loadView() {
        super.loadView()
        
        view = backImage
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    //添加子视图
   private func setupUI(){
    
    //view = backIamge
    view.addSubview(iconImage)
    view.addSubview(label)
    
    //约束
    iconImage.snp_makeConstraints{(make) -> Void in
        
        make.centerX.equalTo(view.snp_centerX)
        make.bottom.equalTo(view.snp_bottom).offset(-180)
        make.width.equalTo(90)
        make.height.equalTo(90)
        
        
    }
    //相对
    label.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(iconImage.snp_bottom).offset(20)
        make.centerX.equalTo(view.snp_centerX)
    }

    
    
    iconImage.layer.cornerRadius = 45
    iconImage.layer.masksToBounds = true
    
}

    //懒加载
    //一个背景
    private lazy var backImage : UIImageView = UIImageView(image: UIImage(named: "Haw"))
    //一个头像
    private lazy var iconImage : UIImageView = UIImageView(image: UIImage(named: "son"))
    //一句话
    private lazy var label : UILabel = {
    
    let lab = UILabel()
        
        lab.text = (userAccountViewModel().userName ?? "") + "  son of Haw"
        lab.font = UIFont.systemFontOfSize(20)
        lab.textAlignment = NSTextAlignment.Center
        lab.textColor = UIColor.greenColor()
        lab.alpha = 0
        lab.sizeToFit()
        return lab
    
    
    }()
    


}
